﻿using System;
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Threading;

namespace ImageConvert
{
    /// <summary>
    /// Interaction logic for Conversion.xaml
    /// </summary>
    public partial class Conversion : Window
    {

        private ConvThread[] Threads;
        private DispatcherTimer _Timer;

        public Conversion()
        {
            InitializeComponent();
            _Timer = new DispatcherTimer();
            _Timer.Interval = TimeSpan.FromSeconds(1);
            _Timer.Tick += new EventHandler(_Timer_Tick);
        }

        private void _Timer_Tick(object sender, EventArgs e)
        {
            int val = 0;
            int compcount = 0;
            foreach (var thread in Threads)
            {
                if (thread.IsFinished) ++compcount;
                val += thread.Counter;
            }
            Progress.Value = val;
            if (compcount == Threads.Length)
            {
                _Timer.IsEnabled = false;
                MessageBox.Show("Conversion finished");
                MainMode();
            }
        }

        private void Expander_Expanded(object sender, RoutedEventArgs e)
        {
            this.Height = 210;
        }

        private void Expander_Collapsed(object sender, RoutedEventArgs e)
        {
            this.Height = 130;
        }

        public void SetupThreads(ObservableCollection<string> _Files)
        {
            Progress.Minimum = 0;
            Progress.Maximum = _Files.Count;
            if (Threads != null)
            {
                Threads = null;
                GC.Collect();
                GC.WaitForPendingFinalizers();
                GC.Collect();
            }
            Threads = new ConvThread[Environment.ProcessorCount];
            for (int i = 0; i < Threads.Length; i++) Threads[i] = new ConvThread();
            int tc = 0;

            foreach (var file in _Files)
            {
                if (tc > Environment.ProcessorCount - 1) tc = 0;
                Threads[tc].AddFile(file);
                ++tc;
            }
        }

        public void ConvMode()
        {
            App.MainWin.Hide();
            this.Show();
        }

        public void MainMode()
        {
            this.Hide();
            App.MainWin.Show();
        }

        public void StartThreads()
        {
            foreach (var thread in Threads) thread.StartThread();
            _Timer.IsEnabled = true;
        }

        private void BtnPause_Click(object sender, RoutedEventArgs e)
        {
            if (BtnPause.Content == "Pause")
            {
                BtnPause.Content = "Resume";
                _Timer.IsEnabled = false;
            }
            else
            {
                BtnPause.Content = "Pause";
                _Timer.IsEnabled = true;
            }
            foreach (var thread in Threads) thread.PauseResrume();
        }

        private void BtnCancel_Click(object sender, RoutedEventArgs e)
        {
            MessageBoxResult res = MessageBox.Show("Stop Conversion?", "Question", MessageBoxButton.YesNo, MessageBoxImage.Question);
            if (res == MessageBoxResult.Yes)
            {
                _Timer.IsEnabled = false;
                foreach (var thread in Threads) thread.StopThread();
                MainMode();
            }
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            e.Cancel = true;
            int comp = 0;
            foreach (var thread in Threads) if (thread.IsFinished) ++comp;
            if (comp != Threads.Length)
            {
                MessageBoxResult res = MessageBox.Show("Stop Conversion?", "Question", MessageBoxButton.YesNo, MessageBoxImage.Question);
                if (res == MessageBoxResult.Yes)
                {
                    _Timer.IsEnabled = false;
                    foreach (var thread in Threads) thread.StopThread();
                    MainMode();
                }
            }
        }
    }
}
